﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CarHelpDesk.Web.Utils;
using System.Transactions;

namespace CarHelpDesk.Web.Models.Repositories.LinqRepositories
{
    public class ImagesRepository:IImagesRepository
    {
         private LinqModelDataContext _db;

         public ImagesRepository()
        {
            _db = new LinqModelDataContext();
#if DEBUG
            _db.Log = DebugTextWriter.Instance;
#endif
        }

        #region IImagesRepository Members

        public void SaveQuestionImage(Image img, int itemID)
        {
            using (var transaction = new TransactionScope())
            {
                ImageQuestion imgQuest = new ImageQuestion();
                
                _db.Images.InsertOnSubmit(img);
                _db.SubmitChanges();

                imgQuest.ImageID = img.ID;
                imgQuest.QuestionID = itemID;

                _db.ImageQuestions.InsertOnSubmit(imgQuest);

                _db.SubmitChanges();
                transaction.Complete();
            }
        }

        public void SaveAnswerID(Image img, int itemID)
        {
            using (var transaction = new TransactionScope())
            {
                ImageAnswers imgAnswer = new ImageAnswers();

                _db.Images.InsertOnSubmit(img);
                _db.SubmitChanges();

                imgAnswer.ImageID = img.ID;
                imgAnswer.AnswerID = itemID;

                _db.ImageAnswers.InsertOnSubmit(imgAnswer);

                _db.SubmitChanges();
                transaction.Complete();
            }
        }

        public Image GetImageByID(int imageID)
        {
            return (from p in _db.Images
                     where p.ID == imageID
                     select p).FirstOrDefault();
        }

        /// <summary>
        /// Get all Images IDs of an existing answer by answerID.
        /// </summary>
        /// <param name="answerID"></param>
        /// <returns></returns>
        public List<int> GetAnswersImagesIDsByAnswerID(int answerID)
        {
            return (from p in _db.ImageAnswers
                    where p.AnswerID == answerID
                    select p.AnswerID).ToList();
        }

        /// <summary>
        /// Delete all images associated with an existing Answer.
        /// </summary>
        /// <param name="answerID"></param>
        public void DeleteAllImagesByAnswerID(int answerID)
        {
            List<ImageAnswers> imageAnswers = (from p in _db.ImageAnswers
                                               where p.AnswerID == answerID
                                               select p).ToList();

            foreach (ImageAnswers imageAnswer in imageAnswers)
            {
                _db.ImageAnswers.DeleteOnSubmit(imageAnswer);
                Image image = (from p in _db.Images
                               where p.ID == imageAnswer.ImageID
                               select p).SingleOrDefault();
                _db.Images.DeleteOnSubmit(image);
            }

            _db.SubmitChanges();
        }

        public List<Image> GetQuestionImages(int itemID)
        {
            List<ImageQuestion> imageQuestions = (from p in _db.ImageQuestions
                                               where p.QuestionID == itemID
                                               select p).ToList();
            List<Image> images = new List<Image>(imageQuestions.Count);
            foreach (var item in imageQuestions)
            {
                Image img = (from p in _db.Images
                             where p.ID == item.ImageID
                             select p).SingleOrDefault();
                images.Add(img);
            }
            return images;
        }

        public List<Image> GetAnswerImages(int itemID)
        {
            List<ImageAnswers> imageAnswers = (from p in _db.ImageAnswers
                                               where p.AnswerID == itemID
                                               select p).ToList();
            List<Image> images = new List<Image>(imageAnswers.Count);
            foreach (var item in imageAnswers)
            {
                Image img = (from p in _db.Images
                             where p.ID == item.ImageID
                             select p).SingleOrDefault();
                images.Add(img);
            }
            return images;
        }

        #endregion
    }
}
